<?php

namespace frontend\controllers;

use Yii;
use frontend\models\UploadForm;
use frontend\models\CompanyModel;
use yii\web\UploadedFile;




class AboutController extends \yii\web\Controller
{

    public $enableCsrfValidation = false; //关闭原有的表单验证类
    //模拟登陆
    public $user_id = 1;
    public function actionIndex()
    {
         $this->redirect(['about']);
    }
   //公司介绍
    public function actionAbout(){
    	return $this->render('about');
    }

    public function actionAdd(){
        $model = new UploadForm;
    	if ($post = Yii::$app->request->post()) {
    	$path = $this->upload($model);
        $path = $path[0];
        print_r($path);
        header("content-type:text/html;charset=utf-8");
        echo "<pre>";
        $post = $_POST;
        unset($post['_csrf']);
        unset($post['UploadForm']);
           
        print_r($post);
        $post['user_id'] = $this->user_id;
        $post['company_img'] =$path;
        $post['company_addtime'] =time();
            Yii::$app->db->createCommand()->insert('seven_company',$post)->execute();
        }else{
            $CompanyModel = CompanyModel::find()->where('user_id='.$this->user_id);
            $com = $CompanyModel->all();
         

                
            if (!empty($com)) {
                return    $this->redirect(['about/sel']);
              
                exit;
           
            }
            
       
    		return $this->render('add',['model'=>$model]);
    	}
	 }


    //公司申請及展示
    public function actionSel(){
         $company = CompanyModel::find()->where('user_id='.$this->user_id);
         $com= $company->asArray()->all();
        $none = false;
        $success = false;
        $error   = false;
         foreach ($com as $key => $value) {
                if ($value['company_status']==0) {
                    $none[] = $value; 
                }elseif($value['company_status']==1){
                    $success[] = $value;  
                }elseif($value['company_status']==2){
                    $error[]=$value;
                }
         }
         $this->render('sel',['none'=>$none,'success'=>$success,'error'=>$error]);


    } 





     //文件上传
    public function upload($model){
        
        $model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
            if ($arr = $model->upload()) {
                return $arr;
            }else{
                return false;
            }
    }

}
